package com.szboanda.hbdc.gwgl.fwgl.controller;

import com.szboanda.beijing.pure.BaseBusinessController;
import com.szboanda.platform.common.utils.MapUtils;
import com.szboanda.platform.common.utils.StringUtils;
import com.szboanda.platform.file.exception.FileException;
import com.szboanda.platform.file.service.IFileInfoService;
import com.szboanda.platform.file.service.IFileService;
import com.szboanda.platform.v3.util.helper.ActionHelper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 梁磊
 * @version 1.0
 * @title 本系统特定文件处理控制器
 * @date 2019-12-05
 */
@Controller
@RequestMapping("/szboanda/hbdc/gwgl/fwgl/controller/customizationFileProcessController")
public class CustomizationFileProcessController extends BaseBusinessController {

    /**
     * 文件信息表Service
     */
    @Autowired
    private IFileInfoService fileInfoService;

    /**
     * 文件操作接口
     */
    @Autowired
    private IFileService fileService;


    /**
     * 通用在线起草文件，在线编辑文件接口
     *
     * @param param {YWSJID:业务数据ID,LXDM:类型代码,ZLXDM:子类型代码,NEWFLAG:是否起草文件,WJMC:文件名称,WJLX:文件后缀}
     * @return
     */
    @RequestMapping(value = "/file/info")
    @ResponseBody
    public Map<String, Object> getFileInfoByBusinessId(@RequestBody Map<String, Object> param) {
        String ywsjid = MapUtils.getString(param, "YWSJID");
        String lxdm = MapUtils.getString(param, "LXDM");
        String zlxdm = MapUtils.getString(param, "ZLXDM");
        if (StringUtils.isEmpty(ywsjid) || StringUtils.isEmpty(lxdm) || StringUtils.isEmpty(zlxdm)) {
            throw new FileException("获取文件失败，缺失必要参数");
        }
        Map<String, Object> searchMap = new HashMap<>(6);
        searchMap.put("YWSJID", ywsjid);
        searchMap.put("LXDM", lxdm);
        searchMap.put("ZLXDM", zlxdm);
        List<Map<String, Object>> fileInfoList = fileInfoService.queryFileInfos(searchMap);
        Map<String, Object> fileInfo = null;
        if (fileInfoList == null || fileInfoList.isEmpty()) {
            boolean newFlag = MapUtils.getBoolean(param, "NEWFLAG");
            if (newFlag) {
                String wjmc = MapUtils.getString(param, "WJMC");
                String wjlx = MapUtils.getString(param, "WJLX");
                XWPFDocument document = new XWPFDocument();
                File file = new File(wjmc + "." + wjlx);
                try (FileOutputStream out = new FileOutputStream(file)) {
                    document.write(out);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    throw new FileException("文件创建失败");
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new FileException("文件创建失败");
                }
                param.put("WJMC", wjmc + "." + wjlx);
                param.put("WJDX", file.length() / 1024);
                param.put("SCRID", ActionHelper.getShareId());
                param.put("SCR", ActionHelper.getShareName());
                param.put("SCSJ", new Date());
                param.put("XZCS", 0);
                param.put("SFSC", 0);
                try (FileInputStream inputStream = new FileInputStream(file)) {
                    fileInfo = fileService.addFile(inputStream, param);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    throw new FileException("文件创建失败");
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new FileException("文件创建失败");
                }
            }
        } else {
            fileInfo = fileInfoList.get(0);
        }
        return fileInfo;
    }

    /**
     *
     * @param
     * @return java.lang.String
     * @author 袁云飞
     * @creed: 根据业务主键和附件类型查询附件是否已经存在
     * @date 2020/1/13 9:21
     */
    @RequestMapping("/checkMainFileByXH")
    @ResponseBody
    public  Map<String,Object> checkMainFileByXH(@RequestBody Map<String,Object> paramMap){
        Map<String,Object> resultMap = new HashMap<>();
        Boolean isExist = false;
        List<Map<String, Object>> fileInfoList = fileInfoService.queryFileInfos(paramMap);
        if (fileInfoList!=null && !fileInfoList.isEmpty()){
            isExist = true;
        }
        resultMap.put("isExist",isExist);
        return resultMap;
    }

}
